﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data; 

namespace websitev2.Core
{
    public class ChartBuilder
    {

        public static string GenerateXMLforUsageArea(int Days, Int64 UserID)
        {
            string _returnXML = ""; 
            DataTable dtGames = Core.SqlAccessCore.ReturnDataTable(Core.GraphCore.ReturnSQLFor30DayUsageByType(UserID, 3, Days));
            DataTable dtSurfing = Core.SqlAccessCore.ReturnDataTable(Core.GraphCore.ReturnSQLFor30DayUsageByType(UserID, 1, Days));
            DataTable dtWork = Core.SqlAccessCore.ReturnDataTable(Core.GraphCore.ReturnSQLFor30DayUsageByType(UserID, 2, Days));

            _returnXML += "<chart  xAxisName='Days' yAxisName='Time'  showValues='0' >";


            _returnXML += "<categories>"; 
            for (int i = Days+1; i > 0; i--)
            {
                if (i == 1) {

                    _returnXML += " <category label='Today' />";
                }
                else
                {
                    _returnXML += " <category label='" + i.ToString() + "' />";
                }
            };
            _returnXML += "</categories>"; 

            _returnXML +="<dataset SeriesName='Playing'>"; 
            foreach (DataRow oRow in dtGames.Rows)
            {
                _returnXML += "<set value ='" + oRow["ElapsedDuration"].ToString() + "' />"; 
            }
                _returnXML +="</dataset>";

                _returnXML += "<dataset SeriesName='Surfing'>";
                foreach (DataRow oRow in dtSurfing.Rows)
                {
                    _returnXML += "<set value ='" + oRow["ElapsedDuration"].ToString() + "' />";
                }
                _returnXML += "</dataset>";

                _returnXML += "<dataset SeriesName='Working'>";
                foreach (DataRow oRow in dtWork.Rows)
                {
                    _returnXML += "<set value ='" + oRow["ElapsedDuration"].ToString() + "' />";
                }
                _returnXML += "</dataset>";


                _returnXML += "</chart>"; 
            return _returnXML; 
        }

        public static string GenerateXMLforAppUsage(Int64 AppID, int Days, bool FilterByUser, Int64 UserID)
        {
            string _returnString = "";

            _returnString +="<chart caption='App usage over the last 30 days' yAxisName='Minutes' xAxisName='Days'>";

            DataTable oTable = Core.SqlAccessCore.ReturnDataTable(GraphCore.ReturnSQLforApp30Days(AppID, false, 0));

            foreach (DataRow oRow in oTable.Rows)
            {
                _returnString += "<set label='' value='" + oRow["ElapsedDuration"].ToString() + "' />"; 
            }

            _returnString += "</chart>"; 

            return _returnString; 

        }

        public static string GenerateXMLforUsagePie(int Working, int Playing, int Browsing)
        {
            string _return = "";

            _return += "<chart caption='Todays activity'>";

            _return += "<set label='working' value='" + Working.ToString() + "' />";
            _return += "<set label='browsing' value='" + Browsing.ToString() + "' />";
            _return += "<set label='playing' value='" + Playing.ToString() + "' />"; 

            _return += "</chart>"; 

            return _return; 
        }


    }
}